/**
 * 
 */
package name.shamansir.tadah.data.scheduling;

import java.util.LinkedHashSet;
import java.util.Set;

import android.text.format.Time;

/**
 * <dl>
 * <dt>Project:</dt> <dd>ta-dah</dd>
 * <dt>Package:</dt> <dd>name.shamansir.tadah.data</dd>
 * </dl>
 *
 * <code>Scheduling</code>
 *
 * <p>Description</p>
 *
 * @author Ulric Wilfred <shaman.sir@gmail.com>
 * @date Feb 21, 2011 9:38:50 PM 
 *
 */
public class Scheduling implements HasDuration {
    
    public enum SchedulingType {
        /* LASTS_FULL_DAY, */
        OCCURS_SINGLE_TIME, // 25 May 2011
        REPEATS_EVERY_DAY, // every day at 17:00
        REPEATS_BY_MINUTES, // every 10 minutes
        REPEATS_BY_WEEKDAYS, // every monday and thursday
        REPEATS_BY_DAYS_IN_MONTH, // every 10th and 15th
        REPEATS_BY_DATES_IN_YEAR, // every 25th may
        REPEATS_BY_MONTHS_IN_YEAR, // every january, march and may
        REPEATS_ODD_OR_EVEN_DAYS, // every odd/even day
        REPEATS_AMOUNT_OF_PERIOD, // every two weeks / every two years / every leap year
        REPEATS_BY_COMPLEX_CONDITIONS // like "every last friday of may" or "every first sunday of any month"
    }
    
    private final Set<SchedulingRule> rules = new LinkedHashSet<SchedulingRule>();
    
    private Scheduling () { }    

    public static Scheduling noRules() {
        return new Scheduling();
    }
    
    public boolean hasRules() {
        return !rules.isEmpty();
    }

    public Run match(Time start, Time end) {
        for (SchedulingRule rule: rules) {
            final Run match;
            if ((match = rule.match(start, end)) != null) return match;
        }
        return null;
    }
    
    public void addRule(SchedulingRule rule) { rules.add(rule); }
    
    public void addRules(Set<SchedulingRule> rules) { this.rules.addAll(rules); }
    
    public Set<SchedulingRule> getRules() { return rules; }
    
    // TODO: getRun?
    
    public void removeOverlaps() {
        this.rules.clear();
        this.rules.addAll(OverlapTest.removeOverlappingRules(rules));
    }

}
